<?php

	class EbToolkit
	{
		static public function getTimePeriod($duration)
		{
			 // Initialisation  
			 $duration = abs($duration);  
			 $converted_duration = array();  
			   
			 // Conversion en semaines  
			 $converted_duration['weeks']['abs'] = floor($duration / (60*60*24*7));  
			 $modulus = $duration % (60*60*24*7);  
			   
			 // Conversion en jours  
			 $converted_duration['days']['abs'] = floor($duration / (60*60*24));  
			 $converted_duration['days']['rel'] = floor($modulus / (60*60*24));  
			 $modulus = $modulus % (60*60*24);  
			   
			 // Conversion en heures  
			 $converted_duration['hours']['abs'] = floor($duration / (60*60));  
			 $converted_duration['hours']['rel'] = floor($modulus / (60*60));  
			 $modulus = $modulus % (60*60);  
			   
			 // Conversion en minutes  
			 $converted_duration['minutes']['abs'] = floor($duration / 60);  
			 $converted_duration['minutes']['rel'] = floor($modulus / 60);  
			 $modulus = $modulus % 60;  
			   
			 // Conversion en secondes  
			 $converted_duration['seconds']['abs'] = $duration;  
			 $converted_duration['seconds']['rel'] = $modulus;

			 return $converted_duration;
		}
	
	
		static function array2object($source)
		{
			$buffer = new StdClass();
			
			foreach($source AS $key => $val)
			{
				$buffer->$key = $val;
			}			
			
			return $buffer;
		}
		
		static function object2array($source)
		{
			$buffer = array();
			
			foreach($source AS $key => $val)
			{
				$buffer[$key] = $val;
			}			
			
			return $buffer;
		}
		
		/**
		* Returns an item of an array
		* $item->$field == $value OR $item[$field] == $value
		*/
		static function getItemByField($source,$field,$value)
		{
			foreach ($source AS $item)
			{
				if (is_array($item))
				{
					if ($item[$field] == $value)
					{
						return $item;
					}
				}
				elseif (is_object($item))
				{
					if ($item->$field == $value)
					{
						return $item;
					}
				}
			}
			
			return null;
		}
		
		/**
		 * Returns a string representation of a boolean
		 */ 
		static public function boolean2string($value)
		{
			return $value === true ? 'true' : 'false';	
		}
	}